package cn.cvs.service;

import cn.cvs.dao.sysUser.FruitMapper;
import cn.cvs.pojo.Fruit;
import cn.cvs.utils.MyBatisUtil;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import java.util.ArrayList;
import java.util.List;

public class FruitService {
    /**
     * 查询Fruit信息
     * @return
     */
    public List<Fruit> list(@Param("pageBegin")Integer pageBegin, @Param("pageSize")Integer pageSize){
        SqlSession sqlSession =null;
        List<Fruit> list=new ArrayList<Fruit>();
        try {

            sqlSession= MyBatisUtil.createSqlSession();
            list =sqlSession.getMapper(FruitMapper.class).list(pageBegin, pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return list;
    }
    /**
     * 根据水果id查询详情
     * @param fruitId
     * @return
     */
    public Fruit FruitList(@Param("fruitId")int fruitId){
        SqlSession sqlSession =null;
        Fruit fruit =new Fruit();
        try {
            sqlSession= MyBatisUtil.createSqlSession();
            fruit =sqlSession.getMapper(FruitMapper.class).FruitList(fruitId);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return fruit;
    }
    /**
     * 添加水果信息
     * @param fruit
     * @return
     */
    public int add(Fruit fruit){
        int count =0;
        SqlSession sqlSession =null;
        try{
            sqlSession= MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(FruitMapper.class).add(fruit);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return  count;
    }
    /**
     * 修改
     * @param fruit
     * @return
     */
    public int update(Fruit fruit){
        int count =0;
        SqlSession sqlSession =null;
        try{
            sqlSession= MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(FruitMapper.class).update(fruit);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return  count;
    }

    /**
     * 根据编号删除信息
     * @param fruitId
     * @return
     */
    public int del(@Param("fruitId")int fruitId){
        int count =0;
        SqlSession sqlSession =null;
        try{
            sqlSession= MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(FruitMapper.class).del(fruitId);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return  count;
    }
}
